<?php
/**
 * 公共模块
 * @author liudaiming
 * @version 2014.3.20
 */
class PublicAction extends Action{
	/**
	 * 用户登录
	 */
	public function doLogin(){
		$email = I('email');
		$password = I('password');
		$verify = strtolower(I('yanzheng'));
		if(md5($verify) != Session('verify'))
		{
			$this->error("验证码错误");
			return ;
		}
		session('verify',null); 
		$passkey = C('PWD_KEY');
		$condition = array('email'=>$email, 'passwd'=>md5($passkey.$password));
		$Model = M('AdminUser');
		$data = $Model->where($condition)->field('id,username,realname,role,state,lastlogin,loginip')->find();
		if(!empty($data)&&$data['state']==0){
			$ip = getIP();
			$value['loginip'] = ip2long($ip);
			$value['lastlogin'] = time();
			$save = $Model->where("id=".$data['id'])->save($value);
		}else{
			$this->error('用户名或密码错误或被锁定');
		}
		
		session('dis_userid',$data['id']);
		session('dis_username',$data['username']);
		session('dis_realname',$data['realname']);
		session('dis_role',$data['role']);
		session('dis_lastime',$data['lastlogin']);
		session('dis_loginip',$data['loginip']);
		$this->success('登录成功，正在为您跳转','/Admin/Index');
	}
	/**
	 * 验证码显示
	 */
	public function verify()
	{
		import('ORG.Util.Image');
		Image::buildImageVerify(4);
	}
    /**
     * 登陆界面
     */
    public function login(){
    	$this->display();
    }
    /**
     * 关于
     */
    public function about(){
    	$js_file = Resource::getStatic(array('jquery','common'),'js');
		$css_file = Resource::getStatic('admin');
		$this->assign('css_file',$css_file);
		$this->assign('js_file',$js_file);
    	$this->display();
    }
    public function notfound(){
    	$this->display();
    }
    
	/**
	 * 生成超级管理员
	 */
	public function createRoot(){
		$username = C('ROOT_USER');
		$realname = 'admin';
		$password = C('ROOT_PWD');
		$email = C('ROOT_EMAIL');
		$passkey = C('PWD_KEY');
		//$passwd = md5();
		$data = array(
			'username'=>$username,
			'realname'=>$realname,
			'email'=>$email,
			'passwd'=>md5($passkey.$password),
			'role'=>2
		);
		$Model = M('AdminUser');
		$check = $Model->where(array('email'=>$email))->count();
		if($check==0){
			$res = $Model->add($data);
		}
		echo '超级用户已经生成';
	}
	public function _empty(){
            $this->display('Public:notfound');
    }
    
}